package com.example.hasee.zhcd.NetUtil;

import android.content.Context;
import android.util.Log;

import com.example.hasee.zhcd.Bean.EventMsg;

import org.greenrobot.eventbus.EventBus;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.Map;

/*
* 基于web Service类型接口的网络请求工具
* */
public class HttpUtils {
    /**
     * 单例对象实例
     */
    private static HttpUtils httpUtils = null;
    private Context context;
    static final String nameSpace = "http://tempuri.org/";
    public static String service_url = "http://121.22.8.229:100/Service.asmx";
    public static String dianxin_url = "http://121.22.8.229:100/Service.asmx";
    public static String liantong_url = "http://121.22.8.229:100/Service.asmx";
    static final String soapAction = "http://tempuri.org";


    public static HttpUtils getInstance(Context context) {
        if (httpUtils == null) {
            httpUtils = new HttpUtils(context);
        }
        return httpUtils;
    }

    public HttpUtils(Context context) {
        this.context = context;
    }

    //get请求
    public void get(final String tag, final Map<String, String> params, final String method) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SoapObject request = new SoapObject(nameSpace, method);
                for (String key : params.keySet()) {
                    request.addProperty(key, params.get(key));
                }
                String result = getresult(request);
                if (result == null) {
                    EventBus.getDefault().post(new EventMsg("Fail", "fuck off", "0"));
                } else {
                    EventBus.getDefault().post(new EventMsg(tag, result, "1"));
                }
            }
        }).start();


    }

    /**
     * 接口请求
     *
     * @param request 请求内容
     * @return 返回json字符串
     */
    private static String getresult(SoapObject request) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER12);
        envelope.bodyOut = request;
        envelope.dotNet = true;
        HttpTransportSE transport = new HttpTransportSE(service_url);
        try {
            transport.call(soapAction, envelope);
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            return result.toString();
        } catch (Exception e) {
            return null;
        }
    }

    /*
    * 新煤网用的是这个
    * */
    private static String getresult(SoapObject request, String method) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        String endPoint = service_url;
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE transport = new HttpTransportSE(endPoint);
        try {
            transport.call(nameSpace + method, envelope);
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            Log.d("resp", result.toString());
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
